export const guid = () => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    let r = (Math.random() * 16) | 0;
    let v = c == 'x' ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
};

export function times(n, iteratee) {
  // 参数校验
  if (!Number.isInteger(n) || n <= 0) {
    return [];
  }

  // 默认迭代函数（当未提供时返回索引）
  const defaultIteratee = i => i;
  const func = iteratee || defaultIteratee;

  // 核心实现
  return Array.from({ length: n }, (_, i) => func(i));
}

export function debounce(func, wait = 200, immediate = true) {
  let timeout;

  return function () {
    const context = this;
    const args = arguments;

    const later = function () {
      timeout = null;
      if (!immediate) func.apply(context, args);
    };

    const callNow = immediate && !timeout;
    clearTimeout(timeout);
    timeout = setTimeout(later, wait);

    if (callNow) func.apply(context, args);
  };
}

export function isObject(obj) {
  return obj !== null && typeof obj === 'object' && !Array.isArray(obj) && Object.prototype.toString.call(obj) === '[object Object]';
}
